package view.component.menu
{
	import core.Language;
	import core.model.MapModel;
	import core.model.PlayerModel;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import framework.view.components.Component;
	import framework.view.components.button.ButtonProxy;
	
	import mapengine.avatars.AvatarBase;
	import mapengine.avatars.IGameAvatar;
	import mapengine.effects.EffectObjectBase;
	import mapengine.effects.IEffectAsset;
	
	import values.avatar.AvatarRoundState;
	
	public class AvatarMenu extends Component implements IEffectAsset
	{
		public function AvatarMenu()
		{
			super();
		}
		
		override protected function get skinReference():String
		{
			return "swf.ui.FactoryBuildingMenuSkin";
		}
		
		[Inject]
		public var playerModel:PlayerModel;
		
		[Inject]
		public var mapModel:MapModel;
		
		private var _avatar:IGameAvatar;
		
		private var _effectObj:EffectObjectBase;

		public function get effectObj():EffectObjectBase
		{
			return _effectObj;
		}

		public function set effectObj(value:EffectObjectBase):void
		{
			_effectObj = value;	
		}
		
		public function get avatar():IGameAvatar
		{
			return _avatar;
		}

		public function set avatar(value:IGameAvatar):void
		{
			_avatar = value ;
			
			if(!_avatar.avatarValue.selectedSkill || !_avatar.avatarValue.selectedSkill.canUseSkill())
			{
				skillBtn.enabled = false;
			}
			if(!_avatar.avatarValue.ultimateSkill || !_avatar.avatarValue.ultimateSkill.canUseSkill())
			{
				ultimateBtn.enabled = false;
			}
			
			if(avatar.avatarValue.currentRoundState != AvatarRoundState.ATTACT)
			{
				cancelBtn.enabled = false;
			}
			
			if(avatar.avatarValue.currentRoundState != AvatarRoundState.MOVE)
			{
				moveBtn.enabled = false;
			}
			
			
			
		}
		
		
		
		protected function get moveBtn():ButtonProxy
		{
			return proxys["move"];
		}
		
		protected function get attactBtn():ButtonProxy
		{
			return proxys["attact"];
		}
		
		protected function get skillBtn():ButtonProxy
		{
			return proxys["skill"];
		}
		
		protected function get ultimateBtn():ButtonProxy
		{
			return proxys["ultimate"];
		}
		
		protected function get cancelBtn():ButtonProxy
		{
			return proxys["cancel"];
		}
		//==========================================================================
		//  Overridden methods: MapBuildingMenu
		//==========================================================================
		override protected function get proxyMapping():Array
		{
			return [ 
				{ skin:"produceBtn", classRef: ButtonProxy, id:"move", text:Language.getText("@building_menu_move_button_label"), onClick:clickToMoveHandler },
				{ skin:"upgradeBtn", classRef: ButtonProxy, id:"attact", text:Language.getText("@attact_button_label"), onClick:clickToAttactHandler },
				{ skin:"stashBtn", classRef: ButtonProxy, id:"skill", text:Language.getText("@skill_button_label"), onClick:clickToSkillHandler },
				{ skin:"moveBtn", classRef: ButtonProxy, id:"ultimate", text:Language.getText("@ultimate_button_label"), onClick:clickToUltimateHandler },
				{ skin:"rotateBtn", classRef: ButtonProxy, id:"standby", text:Language.getText("@standby_button_label"), onClick:clickToStandby },
				{ skin:"sellBtn", classRef: ButtonProxy, id:"cancel", text:Language.getText("@cancel_button_label"), onClick:clickToCancelHandler }
			];
		}
		
		private var _needToStay:Boolean;

		public function get needToStay():Boolean
		{
			return _needToStay;
		}

		public function set needToStay(value:Boolean):void
		{
			_needToStay = value;
		}

		
		override public function initialize():void
		{
			super.initialize();
			
			if (stage)
			{
				stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			}
			else
			{
				this.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			}
			
			
		}
		
		override public function dispose():void
		{
			effectObj.isoWorld.mapMouseClick = true;
			this.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			
			if(effectObj)
			{
				effectObj.dispose();
				_effectObj = null;
			}
			
			if (stage)
			{
				stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			}
			
			if (parent)
			{
				parent.removeChild(this);
			}
			
			super.dispose();
		}
		//==========================================================================
		//  Protected methods
		//==========================================================================
		protected function init():void
		{
			
		}
		
		protected function clickToStandby():void
		{
			_avatar.selectStandby();
			dispose();
		}
		
		protected function clickToAttactHandler():void
		{
			_avatar.selectAttact();
			dispose();
		}
		
		protected function clickToSkillHandler():void
		{
			_avatar.selectSkill();
			dispose();
		}
		
		protected function clickToMoveHandler():void
		{
			_avatar.selectMove();
			dispose();
		}
		
		protected function clickToUltimateHandler():void
		{        
			_avatar.selectUltimate();
			dispose();
		}
		
		protected function clickToCancelHandler():void
		{
			avatar.cancelMove();
			dispose();
		}
		//==========================================================================
		//  Event handlers
		//==========================================================================
		protected function addToStageHandler(event:Event):void
		{
			event.target.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		protected function mouseUpHandler(event:MouseEvent):void
		{
			if(!needToStay)
			{
				event.target.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			
				dispose();
			}
		}

	}
}